package com.opensms.mt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensms.mt.ms.EnviarAction;
import com.util.configuration.ConfigurationParametersManager;
import com.util.exceptions.InternalErrorException;

/**
 * Factoria de <code>MTFacade</code>
 * Necesita los siguientes parametros de configuracion:
 * - MTFactory/theClassName: nombre completo de la clase a instanciar
 * - MTFactory/activeLog: valor con el que se inicializa la clase instanciada
 *
 */
public final class MTFactory {
	protected static final Log log = LogFactory.getLog(MTFactory.class);
	
    private final static String THE_CLASS_NAME_PARAMETER =
        "MTFactory/theClassName";
    private final static String ACTIVE_LOG =
        "MTFactory/activeLog";

    private final static Class theClass = getTheClass();
    private final static boolean logActive = getLogActive();
    
    private MTFactory() {}
    
    private static Class getTheClass() {
    
        Class theClass = null;
    
        try {
        
            String delegateClassName = 
                ConfigurationParametersManager.getParameter(
                    THE_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(delegateClassName);
            
        } catch (Exception e) {
            log.warn("Error al iniciar MTFactory.theClass:" + e.getMessage());
            e.printStackTrace();
        }
        
        return theClass;
        
    }
    
    private static boolean getLogActive() {
        
        boolean logActive = false;    
        try {
        
            String parameterValue = 
                ConfigurationParametersManager.getParameter(
                		ACTIVE_LOG);
            
            logActive = new Boolean(parameterValue);
            
        } catch (Exception e) {
            log.warn("Error al iniciar MTFactory.logActive:" + e.getMessage());
            e.printStackTrace();
        }
        
        return logActive;
        
    }   
    
	public static MTFacade get()
		throws InternalErrorException {
		try {        
			MTFacade inst = (MTFacade) theClass.newInstance();
	        inst.setLogActive(logActive);
	        return inst;
	    } catch (Exception e) {
	        throw new InternalErrorException(e);
	    }
	}
}
